package com.xty.xtyle.frames;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.xty.xtyle.R;
import com.xty.xtyle.base.AbsFragment;
import com.xty.xtyle.db.User;
import com.xty.xtyle.db.UserDao;
import com.xty.xtyle.utils.CommonUtil;

public class MeFragment extends AbsFragment {

    EditText userName, userHeight, userWeight, bustLine, waistLine, hipLine, shoeSize;
    UserDao userDao;
    User user;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    public void initView() {
        userName = findViewById(R.id.name);
        userHeight = findViewById(R.id.height);
        userWeight = findViewById(R.id.weight);
        bustLine = findViewById(R.id.bust);
        waistLine = findViewById(R.id.waist);
        hipLine = findViewById(R.id.hip);
        shoeSize = findViewById(R.id.shoe);

        userDao = new UserDao();
        queryUser();
        findViewById(R.id.user_add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveUser();
            }
        });
    }

    @Override
    public void freshData() {

    }

    //第一进入，查询是否有用户信息，如果有则填充页面的信息
    void queryUser() {
        user = userDao.findFirst();
        if (null == user) return;

        userName.setText(user.getName());
        userHeight.setText(user.getHeight());
        userWeight.setText(user.getWeight());
        bustLine.setText(user.getBustLine());
        waistLine.setText(user.getWaistLine());
        hipLine.setText(user.getHipLine());
        shoeSize.setText(user.getShoeSize());
    }

    //添加或者更新用户信息
    void saveUser() {
        //用户名
        String name = userName.getText().toString();
        //身高
        String height = userHeight.getText().toString();
        //体重
        String weight = userWeight.getText().toString();
        //胸围
        String bust = bustLine.getText().toString();
        //腰围
        String waist = waistLine.getText().toString();
        //臀围
        String hip = hipLine.getText().toString();
        //鞋码
        String shoe = shoeSize.getText().toString();

        if (null == user)
            user = new User();
        user.setName(name);
        user.setHeight(height);
        user.setWeight(weight);
        user.setBustLine(bust);
        user.setWaistLine(waist);
        user.setHipLine(hip);
        user.setShoeSize(shoe);
        user.save();
        CommonUtil.showToast("保存成功");
    }
}
